
using System;
using System.Collections.Generic;
using System.ServiceModel;
using System.Web.UI;
using AdventureWorks.Client.Objects;
using AdventureWorks.Services;
using Xomega.Framework;
using Xomega.Framework.Web;

namespace AdventureWorks.Client.Web
{
    public partial class EmployeeDetails : Page
    {
        private EmployeeObject obj;

        protected void Page_Init(object sender, EventArgs e)
        {
            obj = WebUtil.GetCachedObject<EmployeeObject>(WebUtil.GetCurrentPath(), !WebUtil.IsPostRequest());
            if (!IsPostBack)
            {
                if (IsNew())
                {
                    obj.TemporaryKeyProperty.Required = false;
                    obj.EmployeeIdProperty.Required = false;
                }
                else LoadData();
            }
            pnlMain.DataBind();
            WebUtil.BindToObject(pnlMain, obj);
        }

        private bool IsNew()
        {
            return Request.QueryString.Count == 0;
        }

        private void LoadData()
        {
            obj.TemporaryKeyProperty.SetValue(Request.QueryString["TemporaryKey"]);

            obj.EmployeeIdProperty.SetValue(Request.QueryString["EmployeeId"]);

            IEmployeeService svcEmployee = new AdventureWorks.Services.EmployeeService();
            ErrorList errorList = new ErrorList();
            try
            {
                EmployeeKey inRead = new EmployeeKey();
                obj.ToDataContract(inRead);
                Employee_ReadOutput outRead;
                using (TimeTracker.ServiceCall)
                    outRead = svcEmployee.Read(inRead);
                obj.FromDataContract(outRead);
            }
            catch(Exception ex)
            {
                errorList.MergeWith(ErrorList.FromException(ex));
            }
            try
            {
                EmployeeKey inPayHistory_ReadList = new EmployeeKey();
                obj.ToDataContract(inPayHistory_ReadList);
                IEnumerable<PayHistory> outPayHistory_ReadList;
                using (TimeTracker.ServiceCall)
                    outPayHistory_ReadList = svcEmployee.PayHistory_ReadList(inPayHistory_ReadList);
                obj.PayHistoryList.FromDataContract(outPayHistory_ReadList);
            }
            catch(Exception ex)
            {
                errorList.MergeWith(ErrorList.FromException(ex));
            }
            if (svcEmployee is IDisposable) ((IDisposable)svcEmployee).Dispose();
            errors.List.DataSource = errorList.Errors;
            errors.List.DataBind();
            btnDelete.Enabled = true;
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            obj.Validate(true);
            ErrorList valErr = obj.GetValidationErrors();
            errors.List.DataSource = valErr.Errors;
            errors.List.DataBind();
            if (valErr.HasErrors()) return;

            IEmployeeService svcEmployee = new AdventureWorks.Services.EmployeeService();
            try
            {
                Employee_UpdateInput inUpdate = new Employee_UpdateInput();
                obj.ToDataContract(inUpdate);
                using (TimeTracker.ServiceCall)
                    svcEmployee.Update(inUpdate);
                obj.SetModified(false, true);
                if (IsNew()) Response.Redirect(Request.CurrentExecutionFilePath
                  + "?TemporaryKey=" + obj.TemporaryKeyProperty.Value+ "&EmployeeId=" + obj.EmployeeIdProperty.Value);
            }
            catch(Exception ex)
            {
                errors.List.DataSource = ErrorList.FromException(ex).Errors;
                errors.List.DataBind();
            }
            finally
            {
                if (svcEmployee is IDisposable) ((IDisposable)svcEmployee).Dispose();
            }
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
        }

        protected void btnClose_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void Close()
        {
            // TODO: transfer to the right page
            Server.Transfer("~/_EmployeeSearch.aspx?run=1", false);
        }
    }
}
